home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac-Source 1994 July
/
Mac-Source_July_1994.iso
/
C and C++
/
Libraries
/
CWhoisEngine 1.0
/
CWhoisEngine.c
next >
Wrap
C/C++ Source or Header
|
1992-07-01
|
5KB
|
195 lines
#include "CWhoisEngine.h"
#include "string.h"
#include <Exceptions.h>
#include "TCLUtilities.h"
#include "Global.h"
void CWhoisEngine::IWhoisEngine(long rcvBuffSize, long wdsBuffSize)
{
TRY
{
/* Give function deep access to memory reserves. */
SetAllocation(FALSE);
SetCriticalOperation(TRUE);
/* Lock object reference to keep member variables valid. */
HLock(this);
/* Initialize the wds buffer */
wdsBuffer = NewPtr(wdsBuffSize);
FailMemError();
/* Initialize MacTCP and the tcp stream */
tcp.ioNamePtr = (Ptr) "\P.IPP";
tcp.ioCompletion = NULL;
FailOSErr(PBOpen(&tcp, FALSE)); // Open MacTCP drivers
tcp.csParam.create.rcvBuff = NewPtr(rcvBuffSize);
FailMemError();
tcp.csParam.create.rcvBuffLen = rcvBuffSize;
tcp.csCode = TCPCreate;
FailOSErr(PBControl(&tcp, FALSE)); // Create a TCP stream
/* Unlock object. */
HUnlock(this);
/* Restore normal memory management. */
SetCriticalOperation(FALSE);
SetAllocation(TRUE);
}
CATCH
{
if (wdsBuffer) DisposPtr(wdsBuffer);
if (tcp.csParam.create.rcvBuff) DisposPtr(tcp.csParam.create.rcvBuff);
}
ENDTRY;
}
void CWhoisEngine::SetWhoisServer(ip_addr theAddr)
{
whoisServer = theAddr;
}
char* CWhoisEngine::Whois(char* searchTag)
{
char* marker; // Initialized during string construction
char* answer = NULL;
long answerSize = 1; // SIze of terminating null
RWDS rwds;
TRY
{
/* Give function deep access to memory reserves. */
SetAllocation(FALSE);
SetCriticalOperation(TRUE);
/* Lock object reference to keep member variables valid. */
HLock(this);
/* Open a connection to the whois server on the existing TCP stream. */
tcp.csParam.open.remoteHost = whoisServer;
tcp.csParam.open.remotePort = 43; // port reserved for whois
tcp.csParam.open.localPort = 0; // use any unused local port
tcp.csCode = TCPActiveOpen; // open the conection
FailOSErr(PBControl(&tcp, FALSE));
/* Copy the characters to the write buffer, append a carriage return
and newline (to conform to whois specs). */
rwds.length = strlen(searchTag);
strcpy(wdsBuffer, searchTag);
wdsBuffer[rwds.length++] = '\r';
wdsBuffer[rwds.length++] = '\n';
wdsBuffer[rwds.length++] = '\0';
rwds.buffer = wdsBuffer;
/* Terminate the rwds */
rwds.termination = 0;
/* Send the formatted string to the whois server. */
tcp.csParam.send.wdsPtr = (Ptr) &rwds;
tcp.csCode = TCPSend;
FailOSErr(PBControl(&tcp, FALSE));
/* Receive the data from the whois server. As the amount of data
may be more than the allocated buffer, check if all of the data
has been received and cycle back as necessary. */
do
{
/* Receive the data. */
tcp.csParam.receive.rdsPtr = (Ptr) &rwds;
tcp.csParam.receive.rdsLength = 1; // 1 buffer in rds
tcp.csCode = TCPNoCopyRcv; // Use MacTCP's internal buffers
FailOSErr(PBControl(&tcp, FALSE));
/* Build the results string. */
marker = answer;
answer = NewPtr(rwds.length + answerSize--);
FailMemError();
if (marker)
{
BlockMove(marker, answer, answerSize); // Copy old data to new block of memory
FailMemError();
DisposPtr(marker); // and release the memory it used
FailMemError();
}
BlockMove(rwds.buffer, answer + answerSize, rwds.length);
FailMemError();
answerSize += rwds.length;
answer[answerSize] = '\0'; // Whois data is not null-terminated.
/* Return the buffers to MacTCP. */
tcp.csCode = TCPRcvBfrReturn;
FailOSErr(PBControl(&tcp, FALSE));
/* Check to see if there is any unread data. */
tcp.csCode = TCPStatus;
FailOSErr(PBControl(&tcp, FALSE));
}
while(tcp.csParam.status.connectionState != 14);
/* Close the connection to the whois server. */
tcp.csCode = TCPClose;
FailOSErr(PBControl(&tcp, FALSE));
/* Unlock object. */
HUnlock(this);
}
CATCH
{
tcp.csCode = TCPAbort; // Abort all i/o on this stream and close connection
PBControl(&tcp, FALSE);
if (marker) ForgetPtr(marker);
if (answer) ForgetPtr(answer);
}
ENDTRY;
/* Restore normal memory management. */
SetCriticalOperation(FALSE);
SetAllocation(TRUE);
/* Return the results string. */
return answer;
}
void CWhoisEngine::Dispose(void)
{
/* Lock object reference to keep member variables valid. */
HLock(this);
/* Release the buffers used by MacTCP. */
tcp.csCode = TCPRelease;
PBControl(&tcp, FALSE);
DisposPtr(tcp.csParam.create.rcvBuff);
/* Release the wds buffer. */
DisposPtr(wdsBuffer);
/* Unlock object and delete it. */
HUnlock(this);
inherited::Dispose();
}